﻿package com.tudou.home.common.net
{
    import com.tudou.utils.*;
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;

    public class PictureLoader extends Sprite
    {
        protected var isCompleted:Boolean;
        protected var thisHeight:Number;
        protected var thisWidth:Number;
        protected var backgroundAlpha:Number;
        protected var loader:Loader;
        protected var backgroundColor:uint;

        public function PictureLoader()
        {
            backgroundColor = 0;
            backgroundAlpha = 1;
            thisWidth = 100;
            thisHeight = 75;
            init();
            return;
        }// end function

        public function setBackgroundColorAlpha(param1:uint, param2:Number) : void
        {
            backgroundColor = param1;
            backgroundAlpha = param2;
            drawBackground();
            return;
        }// end function

        public function setWidthHeight(param1:Number, param2:Number) : void
        {
            thisWidth = param1;
            thisHeight = param2;
            drawBackground();
            return;
        }// end function

        protected function ioErrorHlr(event:IOErrorEvent) : void
        {
            return;
        }// end function

        public function load(param1:String) : void
        {
            isCompleted = false;
            if (loader && contains(loader))
            {
                removeChild(loader);
            }
            loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completedHlr);
            loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHlr);
            loader.load(new URLRequest(param1));
            return;
        }// end function

        override public function set height(param1:Number) : void
        {
            setWidthHeight(width, param1);
            return;
        }// end function

        public function get isLoaded() : Boolean
        {
            return isCompleted;
        }// end function

        protected function drawBackground() : void
        {
            RectDisplayUtil.drawRect(graphics, thisWidth, thisHeight, backgroundColor, backgroundAlpha);
            return;
        }// end function

        public function close() : void
        {
            loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, completedHlr);
            loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHlr);
            loader.close();
            return;
        }// end function

        override public function set width(param1:Number) : void
        {
            setWidthHeight(param1, height);
            return;
        }// end function

        protected function completedHlr(event:Event) : void
        {
            isCompleted = true;
            RectDisplayUtil.putChild(loader, thisWidth, thisHeight);
            addChild(loader);
            dispatchEvent(new Event(Event.COMPLETE));
            return;
        }// end function

        protected function init() : void
        {
            drawBackground();
            return;
        }// end function

    }
}
